var express = require('express')
var router = express.Router()
var models = require('../models')
var Op = models.Sequelize.Op
var db = require('../models')
/* GET users listing. */
// 查找所有权限对应的角色的信息 或者 查找被选择的角色的权限信息
router.get('/', async function (req, res, next) {
    try {
        if (req.query) {
            const { categoryId, serviceId } = req.query
            if (categoryId && serviceId) {
                var category_service = await models.Category_Service.findOne({
                    where: { [Op.and]: [{ categoryId, serviceId }] },
                    order: [['id', 'ASC']],
                    include: [
                        {
                            model: models.Bank
                        }, {
                            model: models.Circulation
                        }, {
                            model: models.Type
                        }, {
                            model: models.Risk
                        }, {
                            model: models.Category
                        }, {
                            model: models.Service
                        }
                    ]
                })
                res.json({ category_service })
            } else if (categoryId) {
                var services = await models.Category_Service.findAll({
                    where: { [Op.and]: [{ categoryId }] },
                    order: [['id', 'ASC']],
                    include: [
                        {
                            model: models.Bank
                        }, {
                            model: models.Circulation
                        }, {
                            model: models.Type
                        }, {
                            model: models.Risk
                        }, {
                            model: models.Service
                        }
                    ]
                })
                res.json({ services })
            } else if (serviceId) {
                var categories = await models.Category_Service.findAll({
                    where: { [Op.and]: [{ serviceId }] },
                    order: [['id', 'ASC']],
                    include: [
                        {
                            model: models.Bank
                        }, {
                            model: models.Circulation
                        }, {
                            model: models.Type
                        }, {
                            model: models.Risk
                        }, {
                            model: models.Category
                        }
                    ]
                })
                res.json({ categories })
            } else {
                var category_services = await models.Category_Service.findAll({
                    order: [['id', 'ASC']],
                    include: [
                        {
                            model: models.Bank
                        }, {
                            model: models.Circulation
                        }, {
                            model: models.Type
                        }, {
                            model: models.Risk
                        }, {
                            model: models.Category
                        }, {
                            model: models.Service
                        }
                    ]
                })
                res.json({ category_services })
            }
        }
    } catch (e) {
        console.log(e);
    }
})

// 插入新用户的权限
router.post('/:id', async function (req, res, next) {
    const categoryId = req.params.id
    // const serviceId = JSON.parse(req.query.data)
    const serviceId = req.query.data
    console.log(categoryId, serviceId);
    try {
        var Category = await models.Category_Service.create({
            categoryId, serviceId
        })
        res.json({ Category: Category })
    } catch (e) {
        console.log(e)
    }
})

// 新建业务信息并插入数据库
router.post('/', async function (req, res, next) {
    const { categoryId, serviceId, circulationId, typeId, riskId, timeLimit, bankId } = JSON.parse(req.query.data)
    try {
        var category_service = await models.Category_Service.create({
            categoryId, serviceId, circulationId, typeId, riskId, timeLimit, bankId
        })
        res.json({ category_service })
    } catch (e) {
        console.log(e)
    }
})

// 根据categoryId修改service
router.put('/:id', async function (req, res, next) {
    const id = req.params.id
    // console.log(id);
    const { categoryId, serviceId, circulationId, typeId, riskId, timeLimit, bankId } = JSON.parse(req.query.data)
    try {
        var category_service = await models.Category_Service.update({
            categoryId, serviceId, circulationId, typeId, riskId, timeLimit, bankId
        }, { where: { id } })
        res.json({ category_service })
    } catch (e) {
        console.log(e)
    }
})

// 删除role_right信息
router.delete('/:id', async function (req, res, next) {
    const categoryId = req.params.id
    console.log(categoryId)
    if (categoryId) {
        try {
            const t = await db.sequelize.transaction();
            const category_service = await models.Category_Service.destroy({
                where: {
                    categoryId
                }
            })
            await t.commit()
            if (category_service) {
                res.json({ msg: '删除成功' })
            }
        } catch (error) {
            console.log(error);
            await t.rollback()
        }
    } else {
        res.json({ msg: '不存在该数据' })
    }
})
module.exports = router
